package cn.xiaoyaoji.plugin.html;

import com.sun.xml.internal.ws.util.ByteArrayBuffer;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class HtmlTemplates
{
    private String htmlTemplate;
    
    private String dirTemplate;
    
    private String dirGroupTemplate;
    
    private String dirItemTemplate;
    
    private String groupTemplate;
    
    private String itemTemplate;
    
    private String tableRowTemplate;
    
    private String resultCodeRowTemplate;
    
    private String dataSoruceRowTemplate;
    
    private String cacheRowTemplate;
    
    private String itemTemplateForDSF;
    
    private String itemTemplateForNoDB;
    
    private String interfaceServiceRowTemplate;
    
    public static HtmlTemplates instance = new HtmlTemplates();
    
    public static HtmlTemplates getInstance()
    {
        return instance;
    }
    
    private HtmlTemplates()
    {
        this.htmlTemplate = loadTemplate("html_template.cfg");
        this.dirGroupTemplate = loadTemplate("dir_group_template.cfg");
        this.dirItemTemplate = loadTemplate("dir_item_template.cfg");
        this.groupTemplate = loadTemplate("group_template.cfg");
        this.itemTemplate = loadTemplate("item_template.cfg");
        this.tableRowTemplate = loadTemplate("table_row_template.cfg");
        this.resultCodeRowTemplate = loadTemplate("result_code_row_template.cfg");
        this.dataSoruceRowTemplate = loadTemplate("datasource_table_row_template.cfg");
        this.cacheRowTemplate = loadTemplate("cache_row_template.cfg");
        this.itemTemplateForDSF = loadTemplate("item_templateForDSF.cfg");
        this.itemTemplateForNoDB = loadTemplate("item_templateForNoDB.cfg");
        this.interfaceServiceRowTemplate = loadTemplate("interfaceservice_row_template.cfg");
    }
    
    public String getHtmlTemplate()
    {
        return this.htmlTemplate;
    }
    
    public String getDirTemplate()
    {
        return this.dirTemplate;
    }
    
    public String getDirGroupTemplate()
    {
        return this.dirGroupTemplate;
    }
    
    public String getDirItemTemplate()
    {
        return this.dirItemTemplate;
    }
    
    public String getGroupTemplate()
    {
        return this.groupTemplate;
    }
    
    public String getItemTemplate()
    {
        return this.itemTemplate;
    }
    
    public String getTableRowTemplate()
    {
        return this.tableRowTemplate;
    }
    
    public String getDataSourceRowTemplate()
    {
        return this.dataSoruceRowTemplate;
    }
    
    public String getCacheRowTemplate()
    {
        return this.cacheRowTemplate;
    }
    
    public String getItemTemplateForDSF()
    {
        return this.itemTemplateForDSF;
    }
    
    public String getItemTemplateForNoDB()
    {
        return this.itemTemplateForNoDB;
    }
    
    public String getResultCodeRowTemplate()
    {
        return this.resultCodeRowTemplate;
    }
    
    public String getInterfaceServiceRowTemplate()
    {
        return this.interfaceServiceRowTemplate;
    }
    
    private String loadTemplate(String fileName)
    {
        String ret = readStringFromFile(fileName);
        if ((ret == null) || ("".equals(ret)))
        {
            ret = readStringFromResouce("/" + fileName);
        }
        return ret;
    }
    
    private String readStringFromResouce(String fileName)
    {
        InputStream is = null;
        try
        {
            is = HtmlTemplates.class.getResourceAsStream(fileName);
            ByteArrayBuffer baf = new ByteArrayBuffer();
            
            byte[] buffer = new byte[1024];
            int byteRead = is.read(buffer);
            while (byteRead > 0)
            {
                baf.write(buffer, 0, byteRead);
                byteRead = is.read(buffer);
            }
            String str = new String(baf.getRawData(), 0, baf.size(), "UTF-8");
            return str;
        }
        catch (Exception localException1)
        {
        }
        finally
        {
            if (is != null)
            {
                try
                {
                    is.close();
                }
                catch (Exception localException3)
                {
                }
                is = null;
            }
        }
        return "";
    }
    
    private String readStringFromFile(String fileName)
    {
        InputStream is = null;
        try
        {
            String url = this.getClass().getClassLoader().getResource("").getPath();
            String path = url.substring(0, url.indexOf("classes")) + "plugins/cn.xiaoyaoji.plugin/web/cfg/" + fileName;
            File file = new File(path);
            is = new FileInputStream(file);
            ByteArrayBuffer baf = new ByteArrayBuffer();
            
            byte[] buffer = new byte[1024];
            int byteRead = is.read(buffer);
            while (byteRead > 0)
            {
                baf.write(buffer, 0, byteRead);
                byteRead = is.read(buffer);
            }
            String str = new String(baf.getRawData(), 0, baf.size(), "UTF-8");
            return str;
        }
        catch (Exception localException1)
        {
        }
        finally
        {
            if (is != null)
            {
                try
                {
                    is.close();
                }
                catch (Exception localException3)
                {
                }
                is = null;
            }
        }
        return "";
    }
}
